package com.gccloud.design.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.design.dao.SdRepoLimitDao;
import com.gccloud.design.entity.SdRepoLimitTimeEntity;
import com.gccloud.design.service.LimitTimeConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 环节时限设置
 *
 * @author 冯海琴
 */
@Slf4j
@Service
public class LimitTimeConfigServiceImpl extends ServiceImpl<SdRepoLimitDao, SdRepoLimitTimeEntity> implements LimitTimeConfigService {

    @Resource
    private SdRepoLimitDao limitTimeConfigDao;

    /**
     * 环节时限设置：为某个环节添加环节时限
     */
    @Override
    public boolean addLimitTime(SdRepoLimitTimeEntity sdRepoLimitTimeEntity) {
        int result = 0;
        try {
            List<SdRepoLimitTimeEntity> list = limitTimeConfigDao.getLimitTime(sdRepoLimitTimeEntity.getActivityRepoId());
            if (list.size() == 0) {
                result = limitTimeConfigDao.addLimitTime(sdRepoLimitTimeEntity);
            } else {
                result = limitTimeConfigDao.updateLimitTime(sdRepoLimitTimeEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result > 0;
    }

    /**
     * 获取当前环节的环节时限
     */
    @Override
    public List<SdRepoLimitTimeEntity> getTimeLimit(SdRepoLimitTimeEntity sdRepoLimitTimeEntity) {
        List<SdRepoLimitTimeEntity> sdRepoLimitTimeVo = new ArrayList<>();
        if (!StringUtils.isEmpty(sdRepoLimitTimeEntity.getActivityRepoId())) {
            QueryWrapper<SdRepoLimitTimeEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ACTIVITY_REPO_ID", sdRepoLimitTimeEntity.getActivityRepoId());
            sdRepoLimitTimeVo = limitTimeConfigDao.selectList(queryWrapper);
        }
        return sdRepoLimitTimeVo;
    }
}
